//
//  NewPasswordViewController.m
//  安好
//
//  Created by 冯凯 on 14-11-27.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "NewPasswordViewController.h"

@interface NewPasswordViewController ()
{
    UIButton *rightButton;
}
@end

@implementation NewPasswordViewController

- (void)viewDidAppear:(BOOL)animated
{
    [self.codeTextField resignFirstResponder];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"忘记密码";
    UIBarButtonItem *backBtnImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(back:)];
    backBtnImg.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = backBtnImg;
    
    rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(-20,0, 55, 23);
    [rightButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -30)];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [rightButton setTitle:@"发送" forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(save:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    
    self.codeTextField.frame = CGRectMake(0, 0, kScreenWidth - 70, 46);
    self.codeView.frame = CGRectMake(0, 64, kScreenWidth, 46);
    self.codeUp.frame = CGRectMake(0, 0, kScreenWidth, 1);
    self.codeDown.frame = CGRectMake(0, 45, kScreenWidth, 1);
    
    self.passTextField.frame = CGRectMake(0, 0, kScreenWidth - 70, 46);
    self.passView.frame = CGRectMake(0, 156, kScreenWidth, 46);
    self.passUp.frame = CGRectMake(0, 0, kScreenWidth, 1);
    self.passDown.frame = CGRectMake(0, 45, kScreenWidth, 1);
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)save:(UIButton *)sender
{
    if (self.isLoad == NO) {
        self.isLoad = YES;
        NSString * postURL = [NSString stringWithFormat:@"%@user/updatepw/",kBaseURL];
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        NSDictionary *parameters=@{@"platform": kPlatform,
                                   @"version":kVersion,
                                   @"email":self.userString,
                                   @"checknumber":self.codeTextField.text,
                                   @"newpassword":self.passTextField.text,
                                   };
        [manager POST:postURL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSString *status=[NSString stringWithFormat:@"%@",[responseObject objectForKey:@"status"]];
            self.isLoad = NO;
            if ([status isEqualToString:@"1"]){
                [self.navigationController popToRootViewControllerAnimated:YES];
            }else
            {
                UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"" message:[responseObject objectForKey:@"msg"] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
                alert.tag = 101;
                [alert show];
            }
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        }];
    }

}

- (IBAction)password:(UIButton *)sender
{
    if (self.isLoad == NO) {
        self.isLoad = YES;
        NSString * postURL = [NSString stringWithFormat:@"%@user/resetpw/",kBaseURL];
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        NSDictionary *parameters=@{@"platform": kPlatform,
                                   @"version":kVersion,
                                   @"email":self.userString
                                   };
        [manager POST:postURL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSString *status=[NSString stringWithFormat:@"%@",[responseObject objectForKey:@"status"]];
            self.isLoad = NO;
            if ([status isEqualToString:@"1"]){
                UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"" message:@"重置密码的邮件已发送至您的邮箱，请注意查收！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                alert.tag = 100;
                [alert show];
            }else
            {
                UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"" message:@"您所填写的邮箱未被注册，请重试或注册新用户！" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
                alert.tag = 101;
                [alert show];
            }
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        }];
    }

}

- (IBAction)back:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
@end
